#include "KeyProfile.h"
#include <QXmlStreamWriter>
#include <QDomElement>

namespace vkbrd
{
	void KeyProfile::addSequence(const Sequence& _seq)
	{
		sequences.append(_seq);
	}

	void KeyProfile::findMatchingSequences(BoolFingers _seq, QList<Sequence>& _matchingSequences)
	{
		for(int i = 0; i < sequences.count(); ++i)
			if(sequences[i].match(_seq))
				_matchingSequences.append(sequences[i]);
	}

	const QString KeyProfile::keyProfileXmlElementName("keyProfile");
	const QString profileNameXmlAttrib("name");

	void KeyProfile::serialize( QXmlStreamWriter* _writer )
	{
		_writer->writeStartElement(keyProfileXmlElementName);
		_writer->writeAttribute(profileNameXmlAttrib, name);
		for(int i = 0; i < sequences.count(); ++i)
			sequences[i].serialize(_writer);
		_writer->writeEndElement();
	}

	KeyProfile::KeyProfile( QDomElement& _profileElement )
	{
		name = _profileElement.attribute(profileNameXmlAttrib);

		QDomElement sequenceElement = _profileElement.firstChildElement(Sequence::sequenceXmlElementName);
		for (;!sequenceElement.isNull(); 
			sequenceElement = sequenceElement.nextSiblingElement(Sequence::sequenceXmlElementName)) 
		{
			sequences.append(Sequence(sequenceElement));
		}
	}
	
	void KeyProfile::clearSequences()
	{
		sequences.clear();
	}
}